<template>
	<view class="content">
		<u-button type="primary" @click="login()">登录</u-button>
		<u-button type="primary" @click="loadArea()">加载</u-button>
		<u-button type="primary" @click="login2()">云服务器登录</u-button>
		<u-button type="primary" @click="loaddebug()">加载debug</u-button>
		<text>{{message}}</text>
		<uni-badge class="uni-badge-left-margin" text="123" type="error" />
		<uni-number-box :min="0" :max="9"></uni-number-box>
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{ title }}</text>
		</view>
		<image class="logo" :src="base64image"></image>
		<image class="logo" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603703422475&di=112bea5000d86d33237395596d680479&imgtype=0&src=http%3A%2F%2Fpic1.zhimg.com%2F50%2Fv2-cdfd24d29b4f7c5bbdf38180715f4a95_hd.jpg"></image>
	</view>
</template>

<script>
	// var localCache = require('@/store/localCache.js');
	// let Base64 = require('js-base64').Base64;
import md5 from 'js-md5';
import {userLogout} from '@/common/api.js'

export default {
	data() {
		return {
			title: 'Hello',
			base64image:'data:image/jpeg;base64,',
			message:'这里是结果'
		};
	},
	onLoad() {
		
	},
	mounted() {
		console.log("可以格式化打印，插入内容1:(%s),插入内容2:(%s),后面是", "111111", "22222", '拼接的内容');
	},
	methods: {
		loaddebug: function () {

		},
		login: function() {
			let username = 'admin';
			let passwordmd5 = this.$md5('123456');
			let configdata = {username: username, password: passwordmd5, cgi_type:1};
			let self = this;
			this.$post('/Web/login/login?action=post',  configdata).then(res => {
				console.log('login成功，response: ',res);
				
			}).catch(err => {
				console.log('login失败，error: ',err);
			})
		},
		login2: function() {
			let self = this;
			let username = 'admin';
			let password = '1';
			let logintype = 'phone';
			
			this.$api.userLogin(username, password, logintype).then(res => {
				console.info('登录成功, 即将重定向页面: ',self.redirect);
				let navigationConfig = {
					url:(self.redirect && self.redirect.length > 0)?self.redirect:'/pages/index/index',
					fail: (error) => {
						console.error('重定向错误:',error);
					},
					success: (response) => {
						console.debug('重定向成功');
					}
				};
				// uni.switchTab(navigationConfig)
				// self.loadArea();
			}).catch(err => {
				console.info('登录失败, 请重试');
			})
		},
		loadArea: function(){
			this.$get('/Web/region/get_all_regions?action=get').then(res => {
				console.log('load成功，response: ',res);
			}).catch(err => {
				console.log('load失败，error: ',err);
			})
		},
		
		
		logout: function() {
			this.$api.userLogout().then(res => {
				console.log('index logout 成功');
			}).catch(error => {
				console.log('index logout 失败');
			});
		},
		downjson: function() {
			uni.request({
				url:"https://tcb-dnqcygsynda9lgkf2cb0a-0a732c.service.tcloudbase.com/cloudtest",
				success: (res) => {
					console.log('json:',res.data.data);
				},
				fail: (error) => {
					console.log('error:',error);
				}
			})
		},
		
		getImage: function() {
			var self = this;
			var key = 'image'
			var url = "https://7463-tcb-dnqcygsynda9lgkf2cb0a-0a732c-1303999145.tcb.qcloud.la/images/MacBeeCurtainBackgroundDarkModeImage@3x.png";
			
			uni.downloadFile({
				url: url,
				success: res => {
				
					if (res.statusCode === 200) {
						console.log('下载成功1:',res);
						this.base64image = res.tempFilePath
						console.log('下载成功:',this.base64image);
						uni.setStorage({
							key: key,
							data: res,
							success: function() {}
						});
					}
				}
			});
			
			var aaa = uni.getStorage({
				key: key,
				success: function(res) {
					this.base64image = this.base64image + res.data
					// let base = Base64.encode(res.data);
					// console.log('本地有数据:',base);
				},
				fail: function(res) {
					console.log('本地无数据，开始下载...');
					// 本地没有缓存 需要下载
					uni.downloadFile({
						url: url,
						success: res => {
	
							if (res.statusCode === 200) {
								// this.base64image = this.base64image + res.data
								console.log('下载成功:',base);
								uni.setStorage({
									key: key,
									data: res,
									success: function() {}
								});
							}
						}
					});
				}
			});
			
			console.log('store: ',aaa);
		}
	}
};
</script>

<style>
.content {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.logo {
	height: 200rpx;
	width: 200rpx;
	margin-top: 200rpx;
	margin-left: auto;
	margin-right: auto;
	margin-bottom: 50rpx;
}

.text-area {
	display: flex;
	justify-content: center;
}

.title {
	font-size: 36rpx;
	color: #8f8f94;
}
</style>
